package com.jinxinol.basic.system.web;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;

import com.jinxinol.basic.system.domain.SysUser;
import com.jinxinol.basic.system.service.ISysUserService;
import com.jinxinol.core.common.BaseAction;
import com.jinxinol.core.common.ContextHelper;
import com.jinxinol.core.common.verifycode.VerifyCodeGenerator;

/**
 * 
 * @company Jinxin Computer Corp.
 * @author Chenbing
 * @email wwwchenbing@gmail.com
 * @description 用户登录
 * @date 2012-7-12
 * @upadte 2012-09-06  lxd 
 */
public class LoginAction extends BaseAction {
	private static final long serialVersionUID = -8004959989512432543L;
	private static final Log logger = ContextHelper.getLogger(LoginAction.class);
	
	private SysUser user;
	
	@Resource ISysUserService sysUserService;
	
	/**
	 * 进入登录页面
	 * @return
	 */
	public String loginPage(){
		request.setAttribute("businessType", "访问系统");
		return LOGIN;
	}
	
	/**
	 * 登录
	 * */
	public String login() throws Exception{
		try {
			VerifyCodeGenerator vcGenerator = VerifyCodeGenerator.getInstance();
			if (!vcGenerator.check(request)) {
				message = "验证码输入错误，请重新输入！";
				return LOGIN;
			}
			request.setAttribute("businessType", "用户登录");
			user = sysUserService.userLogin(user.getUname(), user.getPword());
			if(user == null){
				message = "用户名或密码错误！";
				return LOGIN;
			}
			if (user.getUstate().equals("2")) {
				message = "该用户已经停用，请联系管理员！";
				return LOGIN;
			}
			//查询用户的关联信息
			
			user = sysUserService.selectByUserMore(user);
			System.out.println("查询用户的关联信息完成！");
			session.setAttribute("suser", user);
			logger.info(user.getUname() + "登录进入了系统！");
			return SUCCESS;
		} catch (Exception e) {
			logger.error("User login exception occurred", e);
			throw new Exception("用户登录错误。");
		}
	}
	
	public SysUser getUser() {
		return user;
	}
	public void setUser(SysUser user) {
		this.user = user;
	}
}
